<?php

namespace AI;
use GatewayClient\Gateway;

class ChatGpt extends IAI
{

    public function getUrl(): string
    {
        return 'https://api.openai-hk.com/v1/chat/completions';
    }

    public function getData(array $message,string $userID) : string|bool
    {

        return json_encode([
            "max_tokens" => 1200,
            "model" => "gpt-3.5-turbo",
            "temperature" => 0.8,
            "top_p" => 1,
            "presence_penalty" => 1,
            "messages" => self::getMessagesByUid($userID, $message["content"])
        ]);
    }

    public function getHeader(): array
    {
        return array_merge(parent::getHeader(),[
            'Authorization' => 'Bearer hk-zhp0czfz4o9zsa41pohr77dzs4jq2lbbjk550rrsg2aslj8u',
        ]);
    }

    function getReplyMsg(string $reply): false|string
    {
        $reply = json_decode($reply, true);
        if (!isset($reply["choices"]) || !isset($reply["choices"][0]) || !isset($reply["choices"][0]["message"])) {
            return false;
        }

        return $reply["choices"][0]["message"]["content"];
    }
}